package com.bigData.main.TestHDFS;

import lombok.extern.slf4j.Slf4j;
import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.fs.FSDataInputStream;
import org.apache.hadoop.fs.FileStatus;
import org.apache.hadoop.fs.FileSystem;
import org.apache.hadoop.fs.Path;
import org.apache.hadoop.fs.permission.FsPermission;
import org.apache.hadoop.io.IOUtils;

import java.io.*;
import java.net.URI;

@Slf4j
public class TestConnectionHDFS {
    // 创建文件系统对象
    FileSystem fileSystem;

    //需要返回值
    public FileSystem init() {
        try {
            URI uri = new URI("hdfs://192.40.10.130:9000");
            //获取上下文配置信息
            Configuration conf = new Configuration();
            fileSystem = FileSystem.get(uri, conf /*,"root"*/);
            log.info(fileSystem.toString());
        } catch (Exception e) {
            e.printStackTrace();
        }
        System.out.println(fileSystem);
        return fileSystem;
    }

    //创建目录
    public boolean createFilesP(String path) {
        boolean re = false;
        try {
            re = fileSystem.mkdirs(new Path(path), new FsPermission("775"));
            fileSystem.close();
            return re;
        } catch (Exception e) {
            e.printStackTrace();
        }
        return re;
    }


    //上传文件
    public void upload(String src, String dfsSrc) {
        //文件源路径
        try {
            Path srcPath = new Path(src);
            //hdfs存储路径
            Path dfssrc = new Path(dfsSrc);
            //上传文件
            fileSystem.copyFromLocalFile(false, srcPath, dfssrc);
            fileSystem.close();
            System.out.println("*************************************");
            System.out.println("上传成功！");
        } catch (Exception e) {
            e.printStackTrace();
        }

    }


    //下载文件
    public void downloadFile(String dfsSrc, String src) {
        //打开文件流
        //hdfs中目标路径
        try {
            InputStream in = fileSystem.open(new Path(dfsSrc));
            //下载地址
            OutputStream out = new FileOutputStream(src);
            IOUtils.copyBytes(in, out, 4096, true);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }


    //查看目录
    public void Catalog(String dfsSrc) {
        try {
            Path path = new Path(dfsSrc);
            //创建文件目录
            File f = new File("D:/gongfang/mulu");
            //创建字符输出流对象
            //true为会追加内容
            FileWriter outfile = new FileWriter(f, true);

            FileStatus fileStatus = fileSystem.getFileStatus(path);
            System.out.println("*************************************");
            System.out.println("文件根目录: " + fileStatus.getPath());
            System.out.println("这文件目录为：");
            for (FileStatus fs : fileSystem.listStatus(path)) {
                System.out.println(fs.getPath());
                Path path1 = fs.getPath();
                outfile.write(path1.toString());
                outfile.flush();
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
    //任务：将文件目录树写到D：//HDFS_files.txt


//    public void Catalog(String dfsSrc) throws Exception{
//        Path path = new Path(dfsSrc);
//        FileStatus fileStatus = fileSystem.getFileStatus(path);
//        System.out.println("*************************************");
//        System.out.println("文件根目录: "+fileStatus.getPath());
//        InputStream in = fileSystem.open(new Path(fileStatus.getPath()));
//        OutputStream out = new FileOutputStream("d:/HDFS_files.txt");
//        IOUtils.copyBytes(in, out, 4096, true);
//        for(FileStatus fs : fileSystem.listStatus(path)){
//            System.out.println(fs.getPath());
//        }
//    }


    //浏览文件

    public void look(String dfsFile) {
        try {
            Path path = new Path(dfsFile);
            //
            File f = new File("D:/gongfang/file.txt");
            //创建字符输出流对象
            //true为会追加内容
            FileWriter outfile = new FileWriter(f, true);

            FSDataInputStream fsDataInputStream = fileSystem.open(path);
            System.out.println("*************************************");
            System.out.println("浏览文件：");
            int c;
            while ((c = fsDataInputStream.read()) != -1) {
                System.out.print((char) c);
                outfile.write(c);
                outfile.flush();

            }
            fsDataInputStream.close();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

//    InputStream in = fileSystem.open(new Path(dfsSrc));
//    OutputStream out = new FileOutputStream("D:/look.txt");


    //删除文件

    public void delete(String dfsSrc) {
        try {
            Path path = new Path(dfsSrc);
            fileSystem.delete(path, true);
        } catch (Exception e) {
            e.printStackTrace();
        }
        System.out.println("*************************************");
        System.out.println("删除成功！");
    }


}
